package com.css.fxfzypg.manage.building.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.attachment.entity.AttachmentInfoEntity;
import com.css.fxfzypg.base.attachment.service.AttachmentInfoService;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.cache.DivisionCache;
import com.css.fxfzypg.manage.building.repository.entity.BuildingEntity;
import com.css.fxfzypg.manage.building.service.BuildingService;
import com.css.fxfzypg.manage.building.vo.BuildingParams;
import com.css.fxfzypg.util.PlatformObjectUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/building")
@Slf4j
public class BuildingController {
    @Autowired
    BuildingService buildingService;
    @Autowired
    AttachmentInfoService attachmentInfoService;
    @Autowired
    SysAreaService sysAreaService;
    @Autowired
    HttpServletRequest request;

    /**
     * 获取建筑物编号
     * @param province
     * @param city
     * @param area
     * @return
     */
    @GetMapping("/buildCode")
    public RestResponse getBuildCode(String province,String city,String area) {
        RestResponse response = null;
        try{
            String divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(province,"UTF-8"),URLEncoder.encode(city,"UTF-8"),URLEncoder.encode(area,"UTF-8"));
            String divisionId = divisionIds.split("_")[2];//获取县级市id
            String serialNum = DivisionCache.getSerialNum(divisionId);
            response = RestResponse.succeed(serialNum);
            response.setMessage("获取建筑编号成功");
        }catch (Exception e){
            log.error("获取建筑编号失败!",e);
            response=RestResponse.succeed("获取建筑编号失败");
        }
        return response;
    }

    /**
     * 保存建筑物单体数据
     * @param buildingEntity
     * @return
     */
    @PostMapping("/saveBuilding")
    public RestResponse saveBuilding(@RequestBody BuildingEntity buildingEntity) {
        RestResponse response = null;
        try{
            boolean res = buildingService.saveBuilding(buildingEntity);
            if(res){
                response = RestResponse.succeed("保存成功!");
                response.setMessage("保存成功!");
            }else {
                response = RestResponse.fail("保存失败!");
            }
        }catch (Exception e){
            String errormessage = "提交失败!";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }


    @PostMapping("/saveAndSubmit")
    public RestResponse saveAndSubmit(@RequestBody BuildingEntity buildingEntity) {
        RestResponse response = null;
        try{
            boolean res = buildingService.saveAndSubmit(buildingEntity);
            if(res){
                response = RestResponse.succeed("保存并提交成功!");
                response.setMessage("保存并提交成功!");
            }else {
                response = RestResponse.fail("保存并提交失败!");
            }
        }catch (Exception e){
            String errormessage = "保存并提交失败!";
            log.error(errormessage,e);
            response=RestResponse.succeed(errormessage);
        }
        return response;
    }

    /**
     * 查询已提交和未提交列表
     * @param request
     * @param submitFlag
     * @param taskCode
     * @return
     */
    @GetMapping("/querySubmitOrNot")
    public RestResponse querySubmitOrNot(HttpServletRequest request,String submitFlag,String taskCode,String userId) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> map = buildingService.querySubmitOrNot(submitFlag, taskCode,curPage,pageSize,userId);
            response = RestResponse.succeed(map);
        }catch (Exception e){
            log.error("获取列表信息失败!",e);
            response=RestResponse.fail("获取列表信息失败!");
        }
        return response;
    }

  /**
     * 根据id更新提交状态
     * @param
     * @return
     */
    @PostMapping("/updateIsSubmit")
    public RestResponse updateIsSubmit(@RequestBody Map<String,String> map) {
        RestResponse response = null;
        try {
            boolean res = buildingService.updateIsSubmit(map);
            if(res){
                response = RestResponse.succeed("提交成功!");
                response.setMessage("提交成功!");
            }else {
                response = RestResponse.fail("提交失败!");
            }
        } catch (Exception e) {
            String errormessage = "提交失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 根据id查询详情页数据
     * @param id
     * @return
     */
    @GetMapping("/queryData")
    public RestResponse queryData(String id) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            BuildingEntity buildingEntity = buildingService.queryData(id);
            //获取图片
            if(PlatformObjectUtils.isEmpty(buildingEntity.getPicture())){
                json.put("picture","");
            }else{
                List<AttachmentInfoEntity> attachmentInfoEntityList = attachmentInfoService.queryFile(buildingEntity.getPicture());
                json.put("picture",attachmentInfoEntityList);
            }
            json.put("data",buildingEntity);
            json.put("message", "查询详情页成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errormessage = "查询详情页失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }
    /**
     * 修改详情页数据
     * @param buildingEntity
     * @return
     */
    @PostMapping("/modifyData")
    public RestResponse modifyData(@RequestBody BuildingEntity buildingEntity) {
        RestResponse response = null;
        try {
            boolean res = buildingService.modifyData(buildingEntity);
            if(res){
                response = RestResponse.succeed("修改成功!");
                response.setMessage("修改成功!");
            }else {
                response = RestResponse.fail("修改失败!");
            }
        } catch (Exception e) {
            String errormessage = "修改失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 修改并提交
     * @param buildingEntity
     * @return
     */
    @PostMapping("/modifyAndSubmit")
    public RestResponse modifyAndSubmit(@RequestBody BuildingEntity buildingEntity) {
        RestResponse response = null;
        try {
            boolean res = buildingService.modifyAndSubmit(buildingEntity);
            if(res){
                response = RestResponse.succeed("修改并提交成功!");
                response.setMessage("修改并提交成功!");
            }else {
                response = RestResponse.fail("修改并提交失败!");
            }
        } catch (Exception e) {
            String errormessage = "修改并提交失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 数据列表查询
     * @param request
     * @param queryParams
     * @return
     */
   @GetMapping("/queryBuildingList")
    public RestResponse querySamplingData(HttpServletRequest request, BuildingParams queryParams) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> map = buildingService.queryBuildingData(request,queryParams,curPage,pageSize);
            response = RestResponse.succeed(map);
        }catch (Exception e){
            log.error("查询建筑物单体数据列表数据失败!",e);
            response=RestResponse.fail("查询建筑物单体数据列表数据失败!");
        }
        return response;
    }

    /**
     * 根据id删除数据
     * @param
     * @return
     */
    @GetMapping("/deleteById")
    public RestResponse deleteBuilding(String ids) {
        RestResponse response = null;
        try{
            boolean res = buildingService.deleteBuilding(ids);
            if(res){
                response = RestResponse.succeed("删除成功!");
                response.setMessage("删除成功!");
            }else {
                response = RestResponse.fail("删除失败!");
            }
        }catch (Exception e){
            log.error("删除失败!",e);
            response=RestResponse.fail("删除失败!");
        }
        return response;
    }

}